package com.candy.control;

import com.candy.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginservlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("访问后台成功............");
//        设置字符集，处理中文乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        获取前台传递进来的参数
        String username=req.getParameter("username");
        String userpwd=req.getParameter("userpwd");
        System.out.println("username:"+username);
        System.out.println("userpwd:"+userpwd);
        LoginService lg=new LoginService();
//        调用业务逻辑处理层，判断用户名密码是否匹配
       boolean ok= lg.logined(username,userpwd);
       if(ok==true){
           req.getSession().setAttribute("username",username);
//           若用户名密码正确，则跳转到登录成功页面
           req.getRequestDispatcher("WEB-INF/logined.jsp").forward(req,resp);
       }else {
//           若用户名密码不正确，则返回登录页面
           req.getRequestDispatcher("index.jsp").forward(req,resp);
       }




    }
}
